package com.smartcampusbackend.service.impl;

import com.smartcampusbackend.mapper.CampusCardBalanceMapper;
import com.smartcampusbackend.model.CampusCardBalance;
import com.smartcampusbackend.service.CampusCardBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CampusCardBalanceServiceImpl implements CampusCardBalanceService {
    @Autowired
    private CampusCardBalanceMapper campusCardBalanceMapper;

    @Override
    public CampusCardBalance getBalance(Integer userId) {
        return campusCardBalanceMapper.selectByUserId(userId);
    }

    @Override
    public boolean recharge(Integer userId, BigDecimal amount) {
        CampusCardBalance balance = campusCardBalanceMapper.selectByUserId(userId);
        if (balance == null) {
            balance = new CampusCardBalance();
            balance.setUserId(userId);
            balance.setBalance(amount);
            campusCardBalanceMapper.insert(balance);
        } else {
            balance.setBalance(balance.getBalance().add(amount));
            campusCardBalanceMapper.updateById(balance);
        }
        return true;
    }
} 